import {ref, computed} from 'vue'
import {defineStore} from 'pinia'
import {ROLE_ADMIN, ROLE_MASTER, ROLE_NORMAL, ROLE_ROOT, ROLE_USER} from "@/api/const.js";

export const userStore = defineStore('user', () => {
    const doubleCount = computed(() => count.value * 2)
    let loginUser = ref(null)
    let powerName = ref(false);
    let powerNumber = ref(0);
    let root = ref(false);
    let master = ref(false);
    let admin = ref(false);
    let normal = ref(false);
    let user = ref(false);
    let authorization = ref("123123123123");
    let powerTable = [
        {label: "游客", value: ROLE_USER},
        {label: "普通用户", value: ROLE_NORMAL},
        {label: "管理员", value: ROLE_ADMIN},
        {label: "站长", value: ROLE_MASTER},
        {label: "ROOT", value: ROLE_ROOT},
    ]
    let powerTableOption = [
        {type: "info", data: powerTable[0].value, msg: powerTable[0].label},
        {type: "warring", data: powerTable[1].value, msg: powerTable[1].label},
        {type: "primary", data: powerTable[2].value, msg: powerTable[2].label},
        {type: "success", data: powerTable[3].value, msg: powerTable[3].label},
        {type: "danger", data: powerTable[4].value, msg: powerTable[4].label},
    ]

    let powerTableOption2 = [
        {type: "info", data: powerTable[0].label, msg: powerTable[0].label},
        {type: "warring", data: powerTable[1].label, msg: powerTable[1].label},
        {type: "primary", data: powerTable[2].label, msg: powerTable[2].label},
        {type: "success", data: powerTable[3].label, msg: powerTable[3].label},
        {type: "danger", data: powerTable[4].label, msg: powerTable[4].label},
    ]

    function isUser(powerNum) {
        return powerTable[0].value === powerNum
    }

    function isRoot(powerNum) {
        return powerTable[4].value === powerNum
    }

    function isHighEqPower(powerNum) {
        return powerNumber.value >= powerNum
    }

    function logout() {
        loginUser.value = null
        powerName.value = false;
        powerNumber.value = 0;
        root.value = false;
        master.value = false;
        admin.value = false;
        normal.value = false;
        user.value = false;
    }

    function setLoginUser(userFromServer) {
        if (userFromServer) {
            loginUser.value = userFromServer;
            powerName.value = userFromServer.powerName;
            powerNumber.value = userFromServer.powerNumber;
            const power = loginUser.value.powerNumber;
            user.value = true
            if (power >= powerTable[1].value) {
                normal.value = true;
            }
            if (power >= powerTable[2].value) {
                admin.value = true;
            }
            if (power >= powerTable[3].value) {
                master.value = true;
            }
            if (power >= powerTable[4].value) {
                root.value = true;
            }
            loginUser.value.powerNumber = null;
        }
    }

    function delLoginUser() {
        loginUser = ref({})
        powerName = ref(false);
        powerNumber = ref(1);
        root = ref(false);
        master = ref(false);
        admin = ref(false);
        normal = ref(false);
        user = ref(false);
        authorization = ref("");
    }

    return {
        loginUser,
        setLoginUser,
        root,
        master,
        admin,
        normal,
        user,
        authorization,
        powerNumber,
        powerTable,
        isUser,
        isRoot,
        isHighPower: isHighEqPower,
        logout,
        powerTableOption
    }
})
